home *** CD-ROM | disk | FTP | other *** search
/ Freelog 117 / FreelogNo117-OctobreNovembre2013.iso / Programmation / jedit / jedit5.1.0install.exe / {app} / macros / Editing / Greedy_Right.bsh < prev    next >
Text File  |  2013-07-28  |  2KB  |  87 lines

  1. /*
  2.  * Greedy_Right.bsh - If a buffer is using soft tabs,
  3.  * this macro will move the caret tabSize spaces to the right,
  4.  * if all the characters between the caret and the next
  5.  * tab stop are all spaces.  In all other cases, the caret
  6.  * is moved a single character to the right.
  7.  *
  8.  * Copyright (C) 2004 Ollie Rutherfurd <oliver@jedit.org>
  9.  *
  10.  * $Id: Greedy_Right.bsh 5230 2005-07-20 13:31:08Z orutherfurd $
  11.  */
  12.  
  13. /**
  14.  * @param onlyFullTabs if true, the caret will only be moved
  15.  *                     multiple spaces it would constitute
  16.  *                     a 'complete' tab.
  17.  */
  18. void greedyRight(View view, boolean onlyFullTabs)
  19. {
  20.     JEditTextArea textArea = view.getTextArea();
  21.     JEditBuffer buffer = textArea.getBuffer();
  22.     int caret = textArea.getCaretPosition();
  23.     int caretLine = textArea.getCaretLine();
  24.     int lineStart = textArea.getLineStartOffset(caretLine);
  25.     int lineEnd = textArea.getLineEndOffset(caretLine);
  26.  
  27.     if(textArea.getCaretPosition() == buffer.getLength())
  28.         return;
  29.  
  30.     if(buffer.getBooleanProperty("noTabs") == true)
  31.     {
  32.         // if anything is selected, use standard 
  33.         if(textArea.getSelection().length != 0)
  34.         {
  35.             textArea.setCaretPosition(caret+1);
  36.         }
  37.         // if at the end of the line, go to the 
  38.         // start of the next line (+1 for \n)
  39.         else if(caret+1 >= lineEnd)
  40.         {
  41.             textArea.setCaretPosition(caret+1);
  42.         }
  43.         else
  44.         {
  45.             int col = caret - lineStart;
  46.             int tabSize = buffer.getIntegerProperty("tabSize",8);
  47.  
  48.             // unlikely, but just in case
  49.             if(tabSize <= 0)
  50.             {
  51.                 textArea.setCaretPosition(caret+11);
  52.             }
  53.             else
  54.             {
  55.                 int toTabStop = (((col+tabSize)-1) % tabSize) + 1;
  56.                 String chunk = "";
  57.                 if((caret + toTabStop) <= buffer.getLength())
  58.                     chunk = buffer.getText(caret,toTabStop);
  59.                 int count = 0;
  60.                 for(int i=0; i < chunk.length(); i++)
  61.                 {
  62.                     if(' ' != chunk.charAt(i))
  63.                     {
  64.                         break;
  65.                     }
  66.                     count += 1;
  67.                 }
  68.  
  69.                 // if onlyFullTabs must be only spaces to
  70.                 // the tabStop and must have tabSize number 
  71.                 // of spaces to remove them all.
  72.                 if(onlyFullTabs == false || count == tabSize){
  73.                     textArea.setCaretPosition(caret+count);
  74.                 }
  75.                 else{
  76.                     textArea.setCaretPosition(caret+1);
  77.                 }
  78.             }
  79.         }
  80.     }
  81.     else
  82.         textArea.setCaretPosition(caret+1);
  83. }
  84.  
  85. greedyRight(view,true);
  86.  
  87.